package com.queue.cli.initializer;

import com.queue.cli.handler.ConsumerHandler;
import com.queue.common.protocol.MessageDecoder;
import com.queue.common.protocol.MessageEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

/**
 * 消费者初始化器
 */
public class ConsumerInitializer extends ChannelInitializer<SocketChannel> {

    private String destination;

    private ConsumerHandler consumerHandler;

    public ConsumerInitializer(ConsumerHandler consumerHandler) {
        this.consumerHandler = consumerHandler;
    }

    /**
     * 该方法主要是为客户端channel设置编解码器以及消息处理器
     * @param channel
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast("encoder",new MessageEncoder())
                .addLast("decoder",new MessageDecoder())
                .addLast(consumerHandler);
    }
}
